#include "motionblurdialog.h"

/// Paremeter:
/// Return:
/// Action: Listener for the selected transformation direction
///         Sets the filter matrix and shows a preview.
void MotionBlurDialog::choice_clicked()
{
    _selectedChoice = _buttonGroup->checkedId();

    if (_selectedChoice == 0)
    {
        float filterMatrix[81] =
        {
            1, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 1, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 1, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 1, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 1, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 1, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 1, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 1,
        };
        std::copy(std::begin(filterMatrix), std::end(filterMatrix), std::begin(_matrix));

        _filter->set_original_data((int*)_image.bits(), _image.width(), _image.height());
        _filter->set_filter_matrix(filterMatrix, 9);
        _filter->set_bias(0.0);
        _filter->set_factor(1.0 / 9.0);

        _modifiedImage = new QImage((uchar*)_filter->use_filter(),
                                    _image.width(), _image.height(), _image.format());
        _label->setPixmap(QPixmap::fromImage(*_modifiedImage));
    }
    else if (_selectedChoice == 1)
    {
        float filterMatrix[81] =
        {
            0, 0, 0, 0, 0, 0, 0, 0, 1,
            0, 0, 0, 0, 0, 0, 0, 1, 0,
            0, 0, 0, 0, 0, 0, 1, 0, 0,
            0, 0, 0, 0, 0, 1, 0, 0, 0,
            0, 0, 0, 0, 1, 0, 0, 0, 0,
            0, 0, 0, 1, 0, 0, 0, 0, 0,
            0, 0, 1, 0, 0, 0, 0, 0, 0,
            0, 1, 0, 0, 0, 0, 0, 0, 0,
            1, 0, 0, 0, 0, 0, 0, 0, 0,
        };
        std::copy(std::begin(filterMatrix), std::end(filterMatrix), std::begin(_matrix));

        _filter->set_original_data((int*)_image.bits(), _image.width(), _image.height());
        _filter->set_filter_matrix(filterMatrix, 9);
        _filter->set_bias(0.0);
        _filter->set_factor(1.0 / 9.0);

        _modifiedImage = new QImage((uchar*)_filter->use_filter(),
                                    _image.width(), _image.height(), _image.format());
        _label->setPixmap(QPixmap::fromImage(*_modifiedImage));
    }
    else if (_selectedChoice == 2)
    {
        float filterMatrix[81] =
        {
            0, 0, 0, 0, 1, 0, 0, 0, 0,
            0, 0, 0, 0, 1, 0, 0, 0, 0,
            0, 0, 0, 0, 1, 0, 0, 0, 0,
            0, 0, 0, 0, 1, 0, 0, 0, 0,
            0, 0, 0, 0, 1, 0, 0, 0, 0,
            0, 0, 0, 0, 1, 0, 0, 0, 0,
            0, 0, 0, 0, 1, 0, 0, 0, 0,
            0, 0, 0, 0, 1, 0, 0, 0, 0,
            0, 0, 0, 0, 1, 0, 0, 0, 0,
        };
        std::copy(std::begin(filterMatrix), std::end(filterMatrix), std::begin(_matrix));

        _filter->set_original_data((int*)_image.bits(), _image.width(), _image.height());
        _filter->set_filter_matrix(filterMatrix, 9);
        _filter->set_bias(0.0);
        _filter->set_factor(1.0 / 9.0);

        _modifiedImage = new QImage((uchar*)_filter->use_filter(),
                                    _image.width(), _image.height(), _image.format());
        _label->setPixmap(QPixmap::fromImage(*_modifiedImage));
    }
    else
    {
        float filterMatrix[81] =
        {
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            1, 1, 1, 1, 1, 1, 1, 1, 1,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
        };
        std::copy(std::begin(filterMatrix), std::end(filterMatrix), std::begin(_matrix));

        _filter->set_original_data((int*)_image.bits(), _image.width(), _image.height());
        _filter->set_filter_matrix(filterMatrix, 9);
        _filter->set_bias(0.0);
        _filter->set_factor(1.0 / 9.0);

        _modifiedImage = new QImage((uchar*)_filter->use_filter(),
                                    _image.width(), _image.height(), _image.format());
        _label->setPixmap(QPixmap::fromImage(*_modifiedImage));
    }
}

/// Paremeter: img: Small image to show a preview
///            parent: Parent widget
/// Return:
/// Action: Constructor
///         Constructs the widget and sets the members.
MotionBlurDialog::MotionBlurDialog(const QImage &img, QWidget *parent) : TransformationDialog(img, parent)
{
    _mainLayout = new QVBoxLayout;
    _subLayout = new QHBoxLayout;

    _buttonGroup = new QButtonGroup;

    _groupBox = new QGroupBox("Choose a direction");
    _groupBoxLayout = new QVBoxLayout;

    QRadioButton* b0 = new QRadioButton("\\");
    connect(b0, SIGNAL(toggled(bool)), this, SLOT(choice_clicked()));
    _buttonGroup->addButton(b0, 0);
    _buttons.push_back(b0);
    _groupBoxLayout->addWidget(b0);

    QRadioButton* b1 = new QRadioButton("/");
    connect(b1, SIGNAL(toggled(bool)), this, SLOT(choice_clicked()));
    _buttonGroup->addButton(b1, 1);
    _buttons.push_back(b1);
    _groupBoxLayout->addWidget(b1);

    QRadioButton* b2 = new QRadioButton("|");
    connect(b2, SIGNAL(toggled(bool)), this, SLOT(choice_clicked()));
    _buttonGroup->addButton(b2, 2);
    _buttons.push_back(b2);
    _groupBoxLayout->addWidget(b2);

    QRadioButton* b3 = new QRadioButton("-");
    connect(b3, SIGNAL(toggled(bool)), this, SLOT(choice_clicked()));
    _buttonGroup->addButton(b3, 3);
   _buttons.push_back(b3);
   _groupBoxLayout->addWidget(b3);

   _groupBox->setLayout(_groupBoxLayout);

   _subLayout->addWidget(_label);
   _subLayout->addWidget(_groupBox);

   _okButton = new QPushButton("Ok");
   connect(_okButton, SIGNAL(clicked()), this, SLOT(accept()));

   _mainLayout->addLayout(_subLayout);
   _mainLayout->addWidget(_okButton);

   setLayout(_mainLayout);

   _filter = new Filter;

   b0->setChecked(true);

   setWindowTitle("Motion blur options");
}

/// Parameter:
/// Return:
/// Action: Destructor
MotionBlurDialog::~MotionBlurDialog()
{
    delete _mainLayout;
}

/// Paremeter: matrix: filter matrix
/// Return:
/// Action: Copies the selected filter matrix to the parameter.
void MotionBlurDialog::get_filter_matrix(float (&matrix)[81]) const
{
    std::copy(std::begin(_matrix), std::end(_matrix), std::begin(matrix));
}
